/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package progweb.project.Servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import progweb.project.Manager.DBManager;
import progweb.project.Manager.GUIManager;
import progweb.project.Manager.Paziente;
import progweb.project.Manager.Richiamo;
import org.apache.log4j.*;

/**
 *
 * @author Davide Bonomelli
 */
public class PazienteWelcomeSL extends HttpServlet {
    private static org.apache.log4j.Logger log=Logger.getLogger(PazienteWelcomeSL.class);
    RequestDispatcher rd;
    Paziente p;
    String type;
    GUIManager guimanager;    
    
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        response.setContentType("text/html;charset=UTF-8");
        
        //Prendo dati da sessione
        log.info("Recupero attributi dalla sessione");
        this.p = (Paziente) request.getSession().getAttribute("user");
        this.guimanager = (GUIManager) request.getSession().getAttribute("guimanager");
        this.type = (String) request.getSession().getAttribute("type");
        
        //Controllo che non sia nullo
        log.info("Controllo degli attributi");
        if(this.p==null || this.guimanager==null || this.type.equals("medico")){
            log.warn("Sessione inesistente o sceduta");
            request.setAttribute("error", "Sessione inesistente o scaduta.");
            request.setAttribute("page", "LoginServLet?logout=yes");
            request.getSession().invalidate();
            rd=request.getRequestDispatcher("/ErrorServlet");
            rd.forward(request, response);
        }
        
        log.info("Creazione stringhe per la stampa della pagina");
        //Preparo stringa menu
        String strMenu = "<ul id=\"menu\">"
                +"<li><a href=\"LoginServLet?logout=yes\">logout</a></li>"
                +"</ul>";
        //Preparo stringa con dati paziente
        String str= "<div id=\"myData\">\n<h4>Nome:</h4><p>"+this.p.getNome()
                +"</p>\n<h4>Cognome:</h4><p>"+this.p.getCognome()
                +"</p>\n<h4>Anno di nascita:</h4><p>"+this.p.getAnnoDiNascita()
                +"</p>\n</div>\n"
                +"\n<div id=\"vaccini_pazziente\">\n\t<ul>"
                +"\t<h3>Vaccini effettuati</h3>";
        //Prendo i richiami relativi al pazienti
        ArrayList l = DBManager.getRichiamiFromIdPaziente(this.p);
        if(l!=null){
            Iterator i = l.iterator();
            while(i.hasNext()){ //Li aggiungo alla stringa
                Richiamo r = ((Richiamo)i.next());
                str+= "\n\t\t<li>"
                        +"\n\t\t\t<h5>Data: </h5><p>"+ r.getDataSenzaMs() +"</p>"
                        +"\n\t\t\t<h5>Medico: </h5><p>"+ r.getMedico().getNomeCognome() +"</p>"
                        +"\n\t\t</li>";
            }
        } else {
            str+= "\n\t\t<li>"
                        +"\n\t\t\t<h5>Data: </h5><p>-</p>"
                        +"\n\t\t\t<h5>Medico: </h5><p>-</p>"
                        +"\n\t\t</li>";
        }
        str+="\n\t</ul>\n</div>";
        
        log.info("Stampa della pagina");
        this.guimanager.printPageWithContent(str,strMenu); //Stampo a video grazie all'istanza di GUIManager
    }
    
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
